package cn.tedu.web.stm;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "CoreFilter",urlPatterns = "/Stm/*")
public class CoreFilter implements Filter {
    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String uri = request.getRequestURI();
        //获取被拦截的地址
        if("/Stm/login.jsp".equals(uri)||uri.startsWith("/Stm/assets")){
            //通行
            chain.doFilter(req,resp);
            return;
        }
        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if(uid==null){
            response.sendRedirect("/Stm/login.jsp");
            return;
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
